<?php
    include_once ("../ajax/generales.php");
    include_once ("../bussiness/ReporteBR.php");
    include_once ("../entity/ReporteUsu.php");

    $fecha_desde = convertir_fecha($_GET["desde"]);
    $fecha_hasta = convertir_fecha($_GET["hasta"]);

    $rep = new ReporteBR();
    $ticket = $rep->UsuarioReport($fecha_desde, $fecha_hasta);
    $usuarios = $rep->ListaUsuarioXFecha($fecha_desde, $fecha_hasta);
    
    if(sizeof($ticket)!=0){
        $total_ticket = 0;
        foreach ($usuarios as $usu){
            $c = 0;
            $o =0;
            foreach ($ticket as $tic){
                if($usu->usuario == $tic->USUCREACION && $usu->usuario == $tic->USUMODIFICACION){
                    $c++;
                }elseif($usu->usuario == $tic->USUCREACION && $usu->usuario != $tic->USUMODIFICACION){
                    $o++;
                }
            }
            $total_ticket = sizeof($ticket);
            $usu->tickets = $c;
            $usu->porcentaje = ($c / $total_ticket)*100;
        }
        $total = $total_ticket;
        $rep1 = new reporteUsu();
        $rep1->usuario = "Otros";
        $rep1->tickets = $o;
        $rep1->porcentaje = ($o / $total)*100;
        $usuarios[]=$rep1;
        $usu = array();
        
        foreach ($usuarios as $usu2){
            $datos[] = "['".$usu2->usuario."',".number_format($usu2->porcentaje,2)."]";
        }
    }
    
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Highcharts Example</title>
        <script type="text/javascript" src="../extensions/jquery.min.js"></script>
        <script src="../extensions/Highcharts-4.0.4/js/highcharts.js"></script>
        <script src="../extensions/Highcharts-4.0.4/js/modules/exporting.js"></script>
        
        <script type="text/javascript">
            $(function () {
                $('#container').highcharts({
                    chart: {
                        plotBackgroundColor: null,
                        plotBorderWidth: 1,//null,
                        plotShadow: false
                    },
                    title: {
                        text: 'Grafico de Atencion por usuario'
                    },
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: true,
                                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                style: {
                                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                }
                            }
                        }
                    },
                    series: [{
                        type: 'pie',
                        name: 'Browser share',
                        data: [ <?php echo join($datos, ','); ?> ]
                    }]
                });
            });
        </script>
    </head>
    <body>
        <?Php 
            $html = "";
            if(sizeof($datos)==0){
                $html.= "<tr><td colspan='3' align='center'>";
                $html.= "No se encontraron registros";
                $html.= "</td></tr>";
            }else{
                $count = 1;
                
                foreach ($usuarios as $res){
                    $html.="<tr style='font-family:verdana;font-size:12px;height:40px;'>";
                    $html.="<td align='center'>".$count."</td>";
                    $html.="<td align='center'>".$res->usuario."</td>";
                    $html.="<td align='center'>".$res->tickets."</td>";
                    $html.="<td align='center'>".number_format($res->porcentaje,2)."%</td>";
                    $html.="</tr>";
                    $count++;
                }
            }
        ?>
    <center>
        <table style='font-family:verdana;width:90%;' border="0">
            <tr><td align='right' colspan='2'><img src='../images/logo/indecopi2.png' width='250'/></td></tr>
            <tr style='font-family:verdana;font-size:15px;'><td colspan='2' align='center'><h3>Reporte Estadistico de Usuarios</h3></td></tr>
            <tr>
                <td colspan='2'>
                    <table style="width: 100%;">
                        <tr>
                            <td>
                                <b>Desde :</b> &nbsp;&nbsp;<?php echo formato_fecha($_GET["desde"]); ?>
                            </td>
                            <td>
                                <b>Hasta :</b> &nbsp;&nbsp;<?php echo formato_fecha($_GET["hasta"]); ?>
                            </td>
                            <td>
                                <b>Fecha :</b>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr><td colspan='2' ><hr></td></tr>
            <tr>
                <td align='center' width='50%'>
                    <table style="width: 100%;">
                        <tr>
                            <td>
                                <table style='font-family:verdana;width:90%;border-collapse: collapse; border: 1px solid; border-radius: 5px;' border="1">
                                    <tr style='font-family:verdana;background:#ccc;font-size:12px;height:40px;'>
                                        <th>Item</th>
                                        <th>Usuario</th>
                                        <th>Num. de Tickets Atendidos</th>
                                        <th>Porcentaje de Tickets Atendidos</th>
                                    </tr>
                                    <?php echo $html; ?>
                                </table>
                                <br>
                            </td>
                        </tr>
                        <tr style='font-family:verdana;font-size:12px;height:10px;'>
                            <td>
                                <b>Total de Tickets resueltos correctamente: </b> <?php echo $total_ticket; ?>
                            </td>
                        </tr>
                        <tr style='font-family:verdana;font-size:12px;height:10px;'>
                            <td>
                                <b>Total de Tickets resueltos por mas de un operador: </b><?php echo $o; ?>
                            </td>
                        </tr>
                    </table>
                </td>
                <td width='50%'>
                    <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>          
                </td>
            </tr>
        </table>  
    </center>
    </body>